from dataclasses import dataclass
from datetime import datetime
from injector import inject
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder, PipelinePromptTemplate
from langchain_core.messages import AIMessage


@inject
@dataclass
class AppTestHandler:
    def promit_message(self):
        prompt_template = PromptTemplate.from_template("Tell me a joke about {topic}. {date}")
        val = prompt_template.format_prompt(topic="cats", date=datetime.now())

        val1 = prompt_template.invoke({
            "topic": "cats",
            "date": datetime.now()
        })
        print('format', val)
        print('invoke', val1)

        print(val1.to_string())
        print(val1.to_messages())

        prompt_template = ChatPromptTemplate.from_messages([
            ("system", "你是聊天机器人"),
            MessagesPlaceholder('chat_history'),

        ]) + ChatPromptTemplate.from_messages([("user", "给我讲一个关于{topic}的笑话")])

        val2 = prompt_template.invoke({
            "topic": "猫",
            "chat_history": [("human", "给我讲一个笑话"), AIMessage('你好')]
        })
        print(val2)
        print(val2.to_messages())
        print(val2.to_string())

        return val2.to_string()

    def message_template(self):
        # prompt_template = PromptTemplate.from_template("Tell me a joke about {topic}.") + " {date}"
        # val = prompt_template.format_prompt(topic="cats", date=datetime.now())
        instruction_template = PromptTemplate.from_template("你正在模拟{person}")
        full_template = """{instruction}
        {example}
        
        {start}
        """
        full_prompt = PromptTemplate.from_template(full_template)

        example_prompt = PromptTemplate.from_template("""下面是一段例子
        
        Q: {example_q}
        A: {example_a}
        """)

        start_prompt = PromptTemplate.from_template("""请回答我的问题
         Q: {input}
         A: {answer}
        """)
        pipeline_prompt = PipelinePromptTemplate(
            final_prompt=full_template,
            pipeline_prompts=[
                # ("instruction", instruction_template),
                # ("example", example_prompt),
                # ("start", start_prompt)
            ],
        )
        # print(pipeline_prompt)
        # print(pipeline_prompt.format(
        #     person="雷军",
        #     example_q="你最喜欢什么汽车",
        #     example_a="雅迪",
        #     input="你的手机是什么",
        #     answer="iPhone14"
        # ))
        return "123"
